package com.example.teacher.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.teacher.entity.EduCourse;
import com.example.teacher.entity.History;
import com.example.teacher.service.HistoryService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 江晨晖
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/backstage/history")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    //分页查询历史记录
    @PostMapping(value = "getFrontHistory/{page}/{limit}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getFrontHistory(
            @PathVariable long page,
            @PathVariable long limit,
            @RequestBody(required = false) String title,
            HttpServletRequest request
    ){
        if (!StringUtil.isNullOrEmpty(title)){
            title=title.substring(1,title.length()-1);
        }
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (!StringUtil.isNullOrEmpty(memberId)) {
            Page<History> pageCourse = new Page<>(page, limit);
            Map<String, Object> map = historyService.getFrontHistory(pageCourse,title);
            return ResultData.ok().data(map);
        }
        //封装成前端要的数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", null);
        map.put("current", 1);
        map.put("pages", 1);
        map.put("size", 8);
        map.put("total", 0);
        map.put("hasNext", true);
        map.put("hasPrevious", true);
        return ResultData.ok().data(map);
    }
}
